<%-- 
    Document   : results.jsp
    Description: This page draws back and displays results for a submission
    Created on : Apr 24, 2008, 1:58:32 PM
    Author     : Mark Bate
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page session="true"%>
<%@page import="java.util.*" %>
<%@page import="pxgrid_controllers.*" %>
<%@page import="pxgrid_utils.*" %>
<%@page import="pxgrid_entities.*" %>
<%@page import="com.oreilly.servlet.*" %>
<%@page import="java.io.*" %>
<%@page import="java.net.*" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
   <%
        try {
            
            String id = request.getParameter("id");
            
            if(id.length() < 1) {
                out.println("<html>");
                out.println("<meta http-equiv=\"refresh\" content=\"0; url=unauthorized.jsp\">");
                out.println("</html>");
            } else {
                
                PXSubmissionController pxsc = new PXSubmissionController(false);
                
                /**
                NOT YET IMPLEMENTED
                if(request.getParameter("actionToPerform") != null
                    & request.getParameter("jobName") != null
                    & request.getParameter("jobID") != null
                    & request.getParameter("gridName") != null
                    & request.getMethod().equalsIgnoreCase("POST")) {
                    
                    String action = request.getParameter("actionToPerform");
                    String job_name = request.getParameter("jobName");
                    int grid_job_id = Integer.parseInt(request.getParameter("jobID"));
                    String grid_name = request.getParameter("gridName");
                    
                    //out.println("action is: "+action+"<br/>job_name is: "+job_name+"<br/>grid_job_id is:"+grid_job_id+"<br/>grid_name is: "+grid_name+"<br/>");
                    
                    PXJob job = new PXJob();
                    job.setJobName(job_name);
                    job.setGridJobID(grid_job_id);
                    job.setGridController(grid_name);
                    
                    Boolean done = false;
                    
                    try {
                        done = pxsc.performGridActionOnJob(action, job);
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }

                    if(done) {
                        out.println("action was successfully performed!<br/>");
                    } else {
                        out.println("action was unable to be performed<br/>");
                    }
 
                }
                **/
                
                
                Boolean all_jobs_finished = true;
                
                
                PXSubmission submission = pxsc.getResultsForSubmissionID(id);
                ArrayList jobs = submission.getJobs();

                
                String context_path = request.getContextPath();
                String input_path = context_path+"/jobs/"+id;
                String output_path = input_path+"/results";
                
   %>
   <html>
       <head>
           <title>MR Grid Submission Results</title>
           <link href="styles/pxgrid_style.css" rel="stylesheet" type="text/css" />
           <script src="javascript/results.js" type="text/javascript"></script>
       </head>
       <body>
           <a name="top"></a>
           <%@ include file="header_banner.html" %>
           <div id="page">
           <%@ include file="menubar.html" %>
           
           <!-- start content -->
           <div id="content">
               
               <h1 class="title">Results of Submission</h1>
               
               <%
               /**
               NOT YET INMPLEMENTED

               <form id="resultsForm" name="resultsForm" method="POST" action="results.jsp?id= out.println(id); ">
                   <input type="hidden" name="actionToPerform" id="actionToPerform">
                   <input type="hidden" name="jobID" id="jobID">
                   <input type="hidden" name="jobName" id="jobName">
                   <input type="hidden" name="gridName" id="gridName"> 
                   **/
               
                
                        long submission_start_time = 0;
                        long submission_finish_time = 0;
                            
                            
                        %>
                        <div id="comparison_ad">
                        <h2 class="comparison">Results Summary</h2>
                        <p>The table below summarises the results of your MR Grid submission.
                        <br/>You can view more details about a job, as well as the log file and any files it returned by clicking on the "View Details" link for the appropriate job.
                        </p>
                        <p>A row will appear <span class="highlighted">highlighted</span> if the job returned a Z Score of 7 or higher.</p>
                        
                           <table class="resultSummaryTable">
                                <tr>
                                    <td class="headerLeft">PDB File</td>
                                    <td class="header">Space Group</td>
                                    <td class="header">RMSD</td>
                                    <td class="header">Z Score</td>
                                    <td class="header">LLG</td>
                                    <td class="header">Run Time</td>
                                    <td class="header">&nbsp;</td>
                                </tr>
                                <% 
                                    for(int i=0; i < jobs.size(); i++) {
                                        PXJob job = (PXJob) jobs.get(i);
                                        //out.print("\t\t\t\t\t<a href=\"#"+job.getJobName()+"\">"+job.getJobName()+"</a> ");
                                        
                                        if(job.getZScore() >= 7) {
                                            out.println("<tr class='highlighted'>");
                                        } else {
                                            out.println("<tr>");
                                        }
                                        out.println("<td>"+job.getInputPDB()+"</td>");
                                        out.println("<td>"+job.getSpaceGroup()+"</td>");
                                        out.println("<td>"+job.getRMSD()+"</td>");
                                        if (job.getStatus().toLowerCase().matches("finished")) {
                                            out.println("<td>"+job.getZScore()+"</td>");
                                            out.println("<td>"+job.getLogLikelihoodGain()+"</td>");
                                            out.print("<td>");
                                            
                                            if(submission_start_time == 0 || job.getStartTime().getTime() < submission_start_time) {
                                                submission_start_time = job.getStartTime().getTime();
                                            }

                                            if(submission_finish_time == 0 || job.getStopTime().getTime() > submission_finish_time) {
                                                submission_finish_time = job.getStopTime().getTime();
                                            }

                                            long runtime = job.getStopTime().getTime() - job.getStartTime().getTime();

                                            //java puts it in milliseconds, which is always lovely, but we'd prefer in seconds
                                            runtime = runtime/1000;

                                            if(runtime >= 60) {
                                                if(runtime >= 3600) {

                                                    long runtime_min = runtime / 60;
                                                    long runtime_sec = runtime % 60;


                                                    long runtime_hr = runtime_min / 60;
                                                    runtime_min = runtime_min % 60;

                                                    out.print(runtime_hr+" hr");
                                                    if(runtime_hr > 1)
                                                            out.print("s");

                                                    out.print(" "+runtime_min+" min");
                                                    if(runtime_min > 1)
                                                            out.print("s");

                                                    out.print(" "+runtime_sec+" sec");

                                                    if(runtime_sec > 1)
                                                            out.print("s");

                                                } else {
                                                    long runtime_min = runtime / 60;
                                                    long runtime_sec = runtime % 60;

                                                    out.print(runtime_min+" min");
                                                    if(runtime_min > 1)
                                                            out.print("s");

                                                    out.print(" "+runtime_sec+" sec");

                                                    if(runtime_sec > 1)
                                                            out.print("s");
                                                }
                                            } else {
                                                    out.print(runtime+" sec");

                                                    if(runtime > 1)
                                                            out.print("s");

                                            }
                                            out.println("</td>");
                                            
                                        } else {
                                            out.println("<td>N/A</td>");
                                            out.println("<td>N/A</td>");
                                            out.println("<td>In Progress</td>");
                                            all_jobs_finished = false;
                                        }
                                        out.println("<td><a href='#"+job.getJobName()+"'>View Details</a></td>");
                                    }
                    out.println("</table>");
                    out.println("<p><b>Number of Jobs:</b> "+jobs.size());
                if(submission_start_time != 0 && submission_finish_time != 0 && all_jobs_finished) {
                    out.println("<br/>");
                        long submission_runtime = submission_finish_time - submission_start_time;
                        
                        //java puts it in milliseconds, which is always lovely, but we'd prefer in seconds
                        submission_runtime = submission_runtime/1000;
                        
                        if(submission_runtime >= 60) {
                            if(submission_runtime >= 3600) {

                                long submission_runtime_min = submission_runtime / 60;
                                long submission_runtime_sec = submission_runtime % 60;


                                long submission_runtime_hr = submission_runtime_min / 60;
                                submission_runtime_min = submission_runtime_min % 60;

                                out.print("\t\t\t\t\t\t<b>Submission Run Time:</b> "+submission_runtime_hr+" hr");
                                if(submission_runtime_hr > 1)
                                        out.print("s");

                                out.print(" "+submission_runtime_min+" min");
                                if(submission_runtime_min > 1)
                                        out.print("s");

                                out.print(" "+submission_runtime_sec+" sec");

                                if(submission_runtime_sec > 1)
                                        out.print("s");

                            } else {
                                long submission_runtime_min = submission_runtime / 60;
                                long submission_runtime_sec = submission_runtime % 60;

                                out.print("<b>Submission Run Time:</b> "+submission_runtime_min+" min");
                                if(submission_runtime_min > 1)
                                        out.print("s");

                                out.print(" "+submission_runtime_sec+" sec");

                                if(submission_runtime_sec > 1)
                                        out.print("s");
                            }
                        } else {
				out.print("<b>Submission Run Time:</b> "+submission_runtime+" sec");
				
				if(submission_runtime > 1)
					out.print("s");
				
			}
                }
                out.println("</p></div>");
        
                for(int i=0; i < jobs.size(); i++) {

                    PXJob job = (PXJob) jobs.get(i);
                    out.println("\t\t\t<div id=\"JobResultsSection\" class=\"formSection\">" +
                            "<a name=\""+job.getJobName()+"\"></a>" +
                            "\t\t\t\t<div id=\"JobResultsSectionHeader\" class=\"formSectionHeader\">"+
                            job.getJobName()+
                            "</div>" +
                            "\t\t\t\t<div id=\"JobResultsSectionContent\" class=\"formSectionContent\">" +
                            "\t\t\t\t\t<div id=\""+job.getJobName()+"_summary\" class=\"resultsSummarySection\">" +
                            "\t\t\t\t\t\t<p class=\"resultsHeader\">Job Summary:</p>" +
                            "\t\t\t\t\t\t<b>job name:</b> "+job.getJobName()+"<br/>" +
                            "\t\t\t\t\t\t<b>Input MTZ:</b> <a href=\""+input_path+"/"+job.getInputMTZ()+"\">"+job.getInputMTZ()+"</a><br/>" +
                            "\t\t\t\t\t\t<b>Input PDB:</b> <a href=\""+input_path+"/"+job.getInputPDB()+"\">"+job.getInputPDB()+"</a><br/>" +
                            "\t\t\t\t\t\t<b>Space Group:</b> "+job.getSpaceGroup()+"<br/>" +
                            "\t\t\t\t\t\t<b>Status:</b> "+job.getStatus()+"<br/>");
                    

                    if(job.getStatus().toLowerCase().matches("finished")) {
                        out.println("\t\t\t\t\t\t<b>Z Score:</b> "+job.getZScore()+"<br/>" +
                                "\t\t\t\t\t\t<b>LLG:</b> "+job.getLogLikelihoodGain()+"<br/>");
                    }

                    out.println("<br/>\t\t\t\t\t\t<b>Submission Time: </b> "+job.getSubmissionTime().toString()+"<br/>");
                    
                    if(job.getStartTime() != null) {
                        out.println("\t\t\t\t\t\t<b>Start Time: </b>"+job.getStartTime().toString()+"<br/>");
                    }
                    if(job.getStatus().toLowerCase().matches("finished")) {
                        out.println("\t\t\t\t\t\t<b>Stop Time: </b> "+job.getStopTime().toString()+"<br/>");
                        long runtime = job.getStopTime().getTime() - job.getStartTime().getTime();
                        
                        //java puts it in milliseconds, which is always lovely, but we'd prefer in seconds
                        runtime = runtime/1000;
                        
                        if(runtime >= 60) {
                            if(runtime >= 3600) {

                                long runtime_min = runtime / 60;
                                long runtime_sec = runtime % 60;


                                long runtime_hr = runtime_min / 60;
                                runtime_min = runtime_min % 60;

                                out.print("\t\t\t\t\t\t<b>Run Time:</b> "+runtime_hr+" hr");
                                if(runtime_hr > 1)
                                        out.print("s");

                                out.print(" "+runtime_min+" min");
                                if(runtime_min > 1)
                                        out.print("s");

                                out.print(" "+runtime_sec+" sec");

                                if(runtime_sec > 1)
                                        out.print("s");

                                out.print("<br/>");

                            } else {
                                long runtime_min = runtime / 60;
                                long runtime_sec = runtime % 60;

                                out.print("<b>Job Run Time:</b> "+runtime_min+" min");
                                if(runtime_min > 1)
                                        out.print("s");

                                out.print(" "+runtime_sec+" sec");

                                if(runtime_sec > 1)
                                        out.print("s");

                                out.print("<br/>");
                            }
                        } else {
				out.print("<b>Job Run Time:</b> "+runtime+" sec");
				
				if(runtime > 1)
					out.print("s");
				
				out.print("<br/>");
			}
		} 
                    
                    out.println("\t\t\t\t\t</div>");
                    

                    if(job.getStatus().toLowerCase().matches("finished")) {
                        out.println("\t\t\t\t\t<input type=\"button\" value=\"Show Output Files\" name=\""+job.getJobName()+"_filesButton\" id=\""+job.getJobName()+"_filesButton\" class=\"resultsButton\" onClick=\"showFiles('"+job.getJobName()+"')\"/><br/>" +

                            "\t\t\t\t\t<div id=\""+job.getJobName()+"_files\" class=\"outputFilesSection\" style=\"display:none\" >" +
                            "\t\t\t\t\t\t<p class=\"resultsHeader\">Output Files:</p>" +
                            "\t\t\t\t\t\t<b>Output MTZ:</b> <a href=\""+output_path+"/"+job.getOutputMTZ()+"\">"+job.getOutputMTZ()+"</a><br/>" +
                            "\t\t\t\t\t\t<b>Output PDB:</b> <a href=\""+output_path+"/"+job.getOutputPDB()+"\">"+job.getOutputPDB()+"</a><br/>" +
                            "\t\t\t\t\t\t<b>Output Sol:</b> <a href=\""+output_path+"/"+job.getOutputSol()+"\">"+job.getOutputSol()+"</a><br/>" +
                            "\t\t\t\t\t\t<b>Output Sum:</b> <a href=\""+output_path+"/"+job.getOutputSum()+"\">"+job.getOutputSum()+"</a><br/>" +
                            "\t\t\t\t\t</div>");
                    }


                    out.println("\t\t\t\t\t<input type=\"button\" value=\"Show Output Log\" name=\""+job.getJobName()+"_outputButton\" id=\""+job.getJobName()+"_outputButton\" class=\"resultsButton\" onClick=\"showOutput('"+job.getJobName()+"')\"/><br/>" +
                            "\t\t\t\t\t<div id=\""+job.getJobName()+"_output\" class=\"outputLogSection\" style=\"display:none\" >" +
                            "\t\t\t\t\t\t<p class=\"resultsHeader\">Output Log:</p>" +
                            job.getJobStdout() +
                            "\t\t\t\t\t</div>");
                    
                    
                    


                    out.println("<a href=\"#top\"><input type=\"button\" value=\"Back To Top\" class=\"resultsButton\" border=0/></a>" +
                            "\t\t\t\t</div>" +
                            "\t\t\t</div>");
                }
                
                
            /**</form>**/
                %>
            </div>
            </div>
        </body>
    </html>
    
    <%
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            out.println("<html>");
            out.println("<meta http-equiv=\"refresh\" content=\"0; url=unauthorized.jsp\">");
            out.println("</html>");
        }
  
   
   %>